package com.ferdict;

import java.io.StringReader;

import javax.servlet.http.HttpServletRequest;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;

import org.json.JSONException;
import org.json.JSONObject;

@UrlBinding("/login.json")
public class LoginActionBean implements ActionBean {
	private ActionBeanContext context;

	public ActionBeanContext getContext() {
		return context;

	}

	public void setContext(ActionBeanContext context) {
		this.context = context;
	}

	public static final String GET = "GET";
	public static final String POST = "POST";
	public static final String PUT = "PUT";
	public static final String DELETE = "DELETE";

	@DefaultHandler
	public Resolution dispatch() throws JSONException {
		HttpServletRequest req = getContext().getRequest();
		String jsonStr = null;
		if (null != req) {
			if (req.getMethod().equals(GET)) {
				JSONObject o = new JSONObject();
				o.put("token", req.getParameter("userName"));
				jsonStr = o.toString();
			} else if (req.getMethod().equals(PUT)) {
				JSONObject o = new JSONObject();
				jsonStr = o.toString();
			}
		}
		return new StreamingResolution("text/javascript", new StringReader(
				jsonStr));
	}

}
